﻿using System;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using FarseerPhysics;
using FarseerPhysics.Dynamics;
using FarseerPhysics.Collision;
using FarseerPhysics.Factories;
using EpicBall.ScreenSystem;
using FarseerPhysics.Dynamics.Contacts;

namespace EpicBall.Levels.Classes
{
    public class LevelObject
    {
        protected Texture2D _texture;
        protected Body _body;
        protected PhysicsGameScreen _screen;
        protected Color _color = Color.White;
        protected int _width, _height;
        protected bool _active = true;
        protected float _depth = 0.9f;

        public bool isActive { get { return _active; } set { _active = value; } }
        //All the different ways you can create a level object
        public LevelObject(PhysicsGameScreen screen, int width, int height) : this(screen, screen.GameStack.Content.Load<Texture2D>("blank"), width, height) { }
        public LevelObject(PhysicsGameScreen screen, Texture2D texture) : this(screen, texture, texture.Width, texture.Height) { }
        public LevelObject(PhysicsGameScreen screen, Texture2D texture, int width, int height) : this(screen, texture, PhysicsGameScreen.ImageToPolygonBody(screen, texture, screen.World, 1f, 1f), width, height) 
        {
            if (texture.Width == 17 && texture.Height == 3)
            {
                FixtureFactory.AttachRectangle(ConvertUnits.ToSimUnits(width), ConvertUnits.ToSimUnits(height), 10, Vector2.Zero, _body);
            }
        }
        public LevelObject(PhysicsGameScreen screen, Texture2D texture, Body body) : this(screen, texture, body, texture.Width, texture.Height) { }
        public LevelObject(PhysicsGameScreen screen, Texture2D texture, Body body, int width, int height) 
        {
            _texture = texture;
            _body = body;
            _screen = screen;
            _width = width;
            _height = height;
        }

        public Texture2D Texture { get { return _texture; } }
        public Body Body { get { return _body; } set { _body = value; } }
        public Color Color { get { return _color; } set { _color = value; } }

        public virtual void Update(GameTime gameTime) { }
        public virtual void Draw()
        {
            Vector2 displayUnits = ConvertUnits.ToDisplayUnits(_body.Position);
            _screen.GameStack.SpriteBatch.Draw(_texture, new Rectangle((int)(displayUnits.X - _width / 2), (int)(displayUnits.Y - _height / 2), _width, _height), null , _color, 0f, Vector2.Zero, SpriteEffects.None, _depth);
        }


        public bool TestIfMethodOverwritten(string methodName)
        {
            var t = this.GetType();
            var mi = t.GetMethod(methodName, System.Reflection.BindingFlags.DeclaredOnly | System.Reflection.BindingFlags.Instance);
            return mi != null;
        }
        protected int _scoreinc;

        public virtual bool isWarp() { return false; }
        public virtual bool isEntrance() { return false; }
        public bool IgnoreCollision(Fixture f1, Fixture f2, Contact contact) { return false; }
        public virtual bool OnCollision(Fixture f1, Fixture f2, Contact contact) { return true; }
        public virtual String type() { return ""; }
        public virtual int ScoreIncrement { get { return _scoreinc; } set { _scoreinc = value; } }


    }
}
